package com.cskaoyan.service.wx_topic;


import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bean.goods.GoodsExample;
import com.cskaoyan.bean.promote.Topic;
import com.cskaoyan.bean.promote.TopicExample;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wx_topic.TopicBaseParam;
import com.cskaoyan.bean.wx_topic.WXDetailVO;
import com.cskaoyan.bean.wx_topic.WXTopicVO;
import com.cskaoyan.mapper.goods.GoodsMapper;
import com.cskaoyan.mapper.promote.TopicMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.PAForUserEnc;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Service
public class WXTopicServiceImpl implements WXTopicService {
    @Autowired
    TopicMapper topicMapper;
@Autowired
    GoodsMapper goodsMapper;
    @Override
    public PageDataVo listTopic(TopicBaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<Topic> topics = topicMapper.selectByExample(new TopicExample());
        PageDataVo<Topic> pageDataVo = new PageDataVo<>();
        PageInfo<Object> pageInfo = new PageInfo<>();
        pageDataVo.setPage(baseParam.getPage());
        pageDataVo.setPages(pageInfo.getPages());
        pageDataVo.setTotal((int) pageInfo.getTotal());
        pageDataVo.setLimit(baseParam.getLimit());
        pageDataVo.setList(topics);
        return pageDataVo;
    }

    @Override
    public WXDetailVO topicDetail(Integer id) {
        WXDetailVO wxDetailVO = new WXDetailVO();
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria().andIdEqualTo(id);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        Topic topic = topics.get(0);
        GoodsExample goodsExample = new GoodsExample();
       List<Goods> goodsDTOList = new ArrayList<>();
        if (topic == null) return null;
        if (topic.getGoods() != null || !"".equals(topic.getGoods())) {
            Integer[] goods = topic.getGoods();
            for (Integer good : goods) {
                GoodsExample.Criteria goodsCriteria = goodsExample.createCriteria();
                goodsCriteria.andIdEqualTo(good);
                List<Goods> singleGood = goodsMapper.selectByExample(goodsExample);
                goodsDTOList.add(singleGood.get(0));
            }
        }
        wxDetailVO.setGoods(goodsDTOList);
        wxDetailVO.setTopic(topic);
            return wxDetailVO;
        }

    @Override
    public PageDataVo showRelated(Integer id) {
        PageInfo<Object> pageInfo = new PageInfo<>();
        PageHelper.startPage(1,4);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria().andIdEqualTo(id);
        List<Topic> topic = topicMapper.selectByExample(topicExample);
        @NotNull String title = topic.get(0).getTitle();
        String keyword = title.substring(title.length()-1);
        char[] chars = keyword.toCharArray();
        TopicExample relatedTopicExample = new TopicExample();
        TopicExample.Criteria relatedTopicExampleCriteria = relatedTopicExample.createCriteria();
//        relatedTopicExampleCriteria.andTitleLike("%"+chars[0]+"%"+chars[1]+"%");
        relatedTopicExampleCriteria.andTitleLike("%"+chars[0]+"%");
        List<Topic> topicsRelated = topicMapper.selectByExample(relatedTopicExample);

        //set page
        PageDataVo<Topic> pageDataVo = new PageDataVo<>();
        pageDataVo.setPage(1);
        pageDataVo.setLimit(4);
        pageDataVo.setTotal(topicsRelated.size());
        pageDataVo.setPages(topicsRelated.size()/4);
        pageDataVo.setList(topicsRelated);
        return pageDataVo;
    }
}
